package com.rrd.mex.dw.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {

    private static final SimpleDateFormat yyy_mm_dd_format = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat ymd_format = new SimpleDateFormat(
            "yyyy-MM-dd");
    private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private static final TimeZone MexicoTimeZone = TimeZone.getTimeZone("America/Mexico_City");
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public static Long getTS(String datestr) {
        Date date = null;
        long ts = 0;
        if (datestr == null || datestr.trim().equals("")) {
            return ts;
        }
        try {
            date = yyy_mm_dd_format.parse(datestr);
            ts = date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ts;
    }

    /**
     * @Title: getDate
     * @Description: 获取指定日期
     * @param @param type 指定按月还是天如 Calendar.DAY_OF_MONTH
     * @param @param n 增加的天数
     * @param @return
     * @return Date
     * @throws
     */
    public static Date getDate(int type, int n) {
        Calendar cal = Calendar.getInstance();
        cal.add(type, n);
        return cal.getTime();
    }

    public static Date formateToDate(String datestr) {
        return formateToDate(datestr, "yyyy-MM-dd HH:mm:ss");
    }
    public static Date addDay(Date date,int day){
        Calendar cld = Calendar.getInstance(); 
        cld.setTime(date);
        cld.add(Calendar.DAY_OF_MONTH, day);
        return cld.getTime();
    }
    public static long formateToTimestamp(String datestr) {
        Date date = null;
        if (datestr.length() == 10) {
            date = formateToDate(datestr, "yyyy-MM-dd");
        } else {
            date = formateToDate(datestr, "yyyy-MM-dd HH:mm:ss");
        }
        return date.getTime();
    }
    public static String formateDateStrToTimeStr(String datestr,String pattern) {
        Date date = null;
        if (datestr.length() == 10) {
            date = formateToDate(datestr, "yyyy-MM-dd");
            return formateDateStr(date,pattern);
        } else {
           return datestr;
        }
    }
    public static Date formateToDate(String datestr, String pattern) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(datestr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.out.println("datestr=>" + datestr);
            e.printStackTrace();
        }
        return date;
    }

    public static String formateDateStr(Date date, String pattern) {
        String datestr = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            datestr = sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("date==>" + date);
            e.printStackTrace();
        }
        return datestr;

    }

    public static String formateDateStr(Date date) {
        yyy_mm_dd_format.setTimeZone(MexicoTimeZone);
        return yyy_mm_dd_format.format(date);

    }

    public static String toDateStr(Date date) {
        ymd_format.setTimeZone(MexicoTimeZone);
        return ymd_format.format(date);

    }

    public static String toDateStrMs(Date date) {
        df.setTimeZone(MexicoTimeZone);
        return df.format(date);
    }

    /**
     * 比较date1 是否笔date2小
     * @Title: isBefore
     * @Description: TODO
     * @param @param date1
     * @param @param date2
     * @param @return
     * @return boolean
     * @throws
     */
    public static boolean isBefore(Date date1, Date date2) {
        return date1.before(date2);
    }

    public static Date formateNginxDate(String timeLocal) {
        SimpleDateFormat formatter = new SimpleDateFormat(
            "dd/MMM/yyyy:hh:mm:ss Z", Locale.ENGLISH);
        Date date = null;
        try {
            date = formatter.parse(timeLocal);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;

    }
    
    public static String getCurrentDateTime(){
    	Date date = new Date();
    	return formateDateStr(date);
    }
    public static int date_diff(String s1,String s2){
    	Date d1=formateToDate(s1,"yyyy-MM-dd");
    	Date d2=formateToDate(s2,"yyyy-MM-dd");
    	 Calendar c1 = Calendar.getInstance();;
         Calendar c2 = Calendar.getInstance();
         c1.setTime(d1);
         c2.setTime(d2);
       //设置时间为0时   
         c1.set(Calendar.HOUR_OF_DAY, 0);   
         c1.set(Calendar.MINUTE, 0);   
         c1.set(Calendar.SECOND, 0);   
         c2.set(Calendar.HOUR_OF_DAY, 0);   
         c2.set(Calendar.MINUTE, 0);   
         c2.set(Calendar.SECOND, 0); 
         int days = ((int)(c1.getTime().getTime()/1000)-(int)(c2.getTime().getTime()/1000))/3600/24;   
    	return days;
    }

    public static String getUTC6(Long time) {
        TimeZone utc = TimeZone.getTimeZone("UTC");
        SDF.setTimeZone(utc);
        return SDF.format(new Date(time - 6 * 3600 * 1000));
    }

    public static void main(String[] args) throws ParseException {
        long a = 1666548814130L;
        System.out.println(a);
        System.out.println(DateUtil.getUTC6(a));
//       System.out.println(DateUtil.formateDateStrToTimeStr("2018-10-02","yyyy-MM-dd HH:mm:ss"));
//       System.out.println(DateUtil.formateDateStr(new Date(b * 1000L)));
        System.out.println(DateUtil.toDateStrMs(new Date(a)));
    }
}
